package cn.glc.daily.life.web.factory.login.core;

import cn.glc.daily.life.common.config.properties.WeiboAuthProperties;
import cn.glc.daily.life.common.config.properties.WeiboUrlProperties;
import cn.glc.daily.life.infrastructure.po.UserAuthsPo;
import cn.glc.daily.life.infrastructure.repository.mysql.MysqlRepositories;
import cn.glc.daily.life.model.dto.LoginDto;
import cn.glc.daily.life.model.dto.UserSignupDto;
import cn.glc.daily.life.model.dto.weibo.WebBoQueryUserInfoDto;
import cn.glc.daily.life.model.dto.weibo.WebBoUserInfoDto;
import cn.glc.daily.life.model.dto.weibo.WeiBoAccessTokenDto;
import cn.glc.daily.life.model.enums.LoginTypeEnums;
import cn.glc.daily.life.web.convert.WeiBoConvert;
import cn.glc.daily.life.web.factory.login.AbstractThirdLoginFactory;
import cn.glc.daily.life.web.service.user.UserService;
import cn.simon.free.tools.utils.UnirestUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

import static cn.glc.daily.life.infrastructure.repository.mysql.MysqlRepositories.userAuthsRepository;

/**
 * 微博登录
 *
 * @author simon
 * @date 2020/11/10 19:38
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@Component("weiBoWebLogin")
public class ThirdWeiBoWebLogin extends AbstractThirdLoginFactory {

    @Autowired
    private WeiboAuthProperties weiboAuthProperties;
    @Autowired
    private WeiboUrlProperties weiboUrlProperties;
    @Autowired
    private UserService userService;

    @Override
    protected boolean token(LoginDto loginDto) {
        String code = loginDto.getCredential();
        WeiboAuthProperties clone = weiboAuthProperties.clone();
        clone.setCode(code);
        WeiBoAccessTokenDto weiBoAccessTokenDto = UnirestUtils.post(weiboUrlProperties.getOauth2_access_token(), clone, 200L, WeiBoAccessTokenDto.class);
        WebBoUserInfoDto weiBoUserInfoResponse = UnirestUtils.get(weiboUrlProperties.getUser_authorize(),
                WebBoQueryUserInfoDto.builder()
                        .uid(weiBoAccessTokenDto.getUid())
                        .access_token(weiBoAccessTokenDto.getAccessToken()).build(), WebBoUserInfoDto.class);
        //code当成密码,每次登陆都会更新该code
        UserSignupDto userSignupDto = WeiBoConvert.INSTANCE.weiBoUserInfoToUserSignUpDto(weiBoUserInfoResponse, code);
        UserAuthsPo userAuthsPo = userAuthsRepository.findByIdentifyAndType(userSignupDto.getIdentify(), userSignupDto.getType());
        if (Objects.nonNull(userAuthsPo)) {
            //更新密码
            userAuthsPo.setCredential(code);
            userAuthsRepository.updateById(userAuthsPo);
        } else {
            //如果账号不存在,注册用户
            userService.signup(userSignupDto);
        }
        loginDto.setIdentify(userSignupDto.getIdentify());
        loginDto.setCredential(code);
        loginDto.setType(LoginTypeEnums.WEI_BO_WEB);
        return true;
    }

    @Override
    public Long login(LoginDto loginDto) {
        if (token(loginDto)) {
            UserAuthsPo userAuthsPo = MysqlRepositories.userAuthsRepository.findByIdentifyAndType(loginDto.getIdentify(), loginDto.getType().getName());
            if (Objects.nonNull(userAuthsPo) && StringUtils.equals(loginDto.getCredential(), userAuthsPo.getCredential())) {
                return userAuthsPo.getUid();
            }
        }
        return null;
    }

    @Override
    public Boolean match(LoginDto loginDto) {
        return false;
    }
}
